package com.mediacloud.app.newsmodule.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.mediacloud.app.assembly.views.CircularImageViewX;
import android.widget.TextView;
import com.mediacloud.app.newsmodule.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yoake on 2017/7/10.
 */

public class SubPanelPopWin extends PopupWindow {
    private View contentView;
    private GridView mGridView;
    private TextView mChange, mOkey;
    private UserSubAdapter adapter;

    public SubPanelPopWin(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.sub_panel_layout, null);
        mGridView = contentView.findViewById(R.id.userGridView);
        adapter = new UserSubAdapter(context);
        mGridView.setAdapter(adapter);
        mChange = contentView.findViewById(R.id.subPanelChange);
        mOkey = contentView.findViewById(R.id.subPanelOkay);
        this.setContentView(contentView);
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        ColorDrawable dw = new ColorDrawable(0000000000);
        this.setBackgroundDrawable(dw);
    }


//    public void setUserData() {
//        ;
//        List<TestUser> mUsers = new ArrayList<>();
//        mUsers.add(new TestUser("张1三", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("李1四", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("王1五", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("赵1六", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("张2三", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("李2四", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("王2五", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("赵2六", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("张3三", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("李3四", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("王3五", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        mUsers.add(new TestUser("赵4六", "http://p3.gexing.com/G1/M00/8E/18/rBACFFJ3jrCyZL9PAAAjaXR4JHE026_200x200_3.jpg"));
//        adapter.setData(mUsers);
//    }

    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            showAtLocation(parent.getRootView(), Gravity.CENTER, 0, 0);
        } else {
            this.dismiss();
        }
    }

    class UserSubAdapter extends BaseAdapter {
        private Context mContext;
        private List<TestUser> mUsers;
        private List<TestUser> mChecked;

        public UserSubAdapter(Context mContext) {
            this.mContext = mContext;
            mUsers = new ArrayList<>();
            mChecked = new ArrayList<>();
        }

        protected void setData(List<TestUser> mUsers) {
            this.mUsers = mUsers;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mUsers.size();
        }

        @Override
        public Object getItem(int position) {
            return mUsers.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(mContext, R.layout.user_sub_item, null);
                holder.mUserIco = convertView.findViewById(R.id.sub_ico);
                holder.mUserName = convertView.findViewById(R.id.sub_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final TestUser mUser = mUsers.get(position);
            holder.mUserIco.load(mUser.getAvatar());
            holder.mUserName.setText(mUser.getName());
            if(mChecked.contains(mUser)){
                holder.mUserName.setTextColor(0xffff0000);
                holder.mUserIco.setBorderColor(0xffff0000);
            }else{
                holder.mUserName.setTextColor(0xff444444);
                holder.mUserIco.setBorderColor(0x00000000);
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!mChecked.contains(mUser)) {
                        mChecked.add(mUser);
                    } else {
                        mChecked.remove(mUser);
                    }
                    notifyDataSetChanged();
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        TextView mUserName;
        CircularImageViewX mUserIco;
    }

    class TestUser {
        private String name;
        private String avatar;

        public TestUser(String name, String avatar) {
            this.name = name;
            this.avatar = avatar;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }
    }
}